/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.InventoryControl;

import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import merlion.scm.icm.session.InventoryControlSessionBeanLocal;
import merlion.scm.rmpm.entity.RawMaterialInventoryBatch;
import merlion.scm.rmpm.entity.RawMaterialInventoryRecord;
import org.primefaces.context.RequestContext;

@ManagedBean(name = "rmiManagerBean")
@ViewScoped
public class RawMaterialInventoryManagerBean {

    @EJB
    private InventoryControlSessionBeanLocal icSessionBean;
    private RawMaterialInventoryRecord rmir;
    private RawMaterialInventoryBatch rmib;
    private String statusMsg;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------    
    public RawMaterialInventoryManagerBean() {
    }

    @PostConstruct
    public void init() {
       
        rmir = null;
        rmib = null;
    }

    //--------------------------------------------------------------------------
    //                              Manager Bean Methods
    //--------------------------------------------------------------------------
    public Collection<RawMaterialInventoryRecord> getAllRawMaterialInventoryRecord() {
        return icSessionBean.getAllRawMaterialInventoryRecord();
    }
    
    public void viewRawMaterialInventoryRecord(ActionEvent event){
        rmir = (RawMaterialInventoryRecord) event.getComponent().getAttributes().get("selected");
        rmib = null;
    }
    
    public void viewBatchLocation (ActionEvent event){
        rmib = (RawMaterialInventoryBatch) event.getComponent().getAttributes().get("selected");
    }

    public void updateRawMaterialOptimalLevel(ActionEvent event) {
        rmir = (RawMaterialInventoryRecord) event.getComponent().getAttributes().get("selected");
    }

    public String saveRawMaterialOptimalLevel() {

        RequestContext context = RequestContext.getCurrentInstance();

        //Validate
        boolean valid = true;
        if (rmir.getOptimalStock() < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Optimal Stock cannot be negative", ""));
            valid = false;
        }
        context.addCallbackParam("valid", valid);

        if (valid) {
            rmir = icSessionBean.saveRawMaterialInventoryRecord(rmir);

            if (rmir != null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Raw Material Inventory Record " + rmir.getRawMaterial().getName() + " updated successfully.", ""));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Raw Material update has failed. Please try again.", ""));
            }
        }
        
        return null;
    }

    //--------------------------------------------------------------------------
    //                              Getter and Setter
    //--------------------------------------------------------------------------
    public RawMaterialInventoryRecord getRmir() {
        return rmir;
    }

    public void setRmir(RawMaterialInventoryRecord rmir) {
        this.rmir = rmir;
    }

    public RawMaterialInventoryBatch getRmib() {
        return rmib;
    }

    public void setRmib(RawMaterialInventoryBatch rmib) {
        this.rmib = rmib;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }
}
